<template>
  <div class="app-container">
    <div
      v-if="formPosition.closed == 1"
      style="position: relative;
      width: 100%;">
      <el-image
        class="btn-interview"
        :src="require(`@/assets/images/pdred.png`)"
        type="danger">
      </el-image>
    </div>
    <el-form
      class="noteAdd"
      label-position="top"
      :model="formPosition"
      ref="queryPositionForm"
      size="small"
      :inline="true"
      label-width="108px"
      style="border-bottom: 2px solid #20a0ff;">
      <div style="margin-top: 20px;display: flex;justify-content: center;">
          <el-form-item label-width="120px" label="Company Name" prop="companyName">
            <el-input
              v-model="formPosition.companyName"
              placeholder=""
              disabled
            />
          </el-form-item>
          <el-form-item label="Position" prop="title">
            <el-input
              v-model="formPosition.title"
              placeholder=""
              disabled
            />
          </el-form-item>

      </div>
      <div style="display: flex;justify-content: center;">
          <el-form-item label-width="120px" label="Salary" prop="salary">
            <el-input
              v-model="formPosition.salary"
              placeholder=""
              disabled
            />
          </el-form-item>
          <el-form-item label="Salary Upper" prop="salaryupper">
            <el-input
              v-model="formPosition.salaryupper"
              placeholder=""
              disabled
            />
          </el-form-item>
      </div>
      <el-row style="display: flex;justify-content: center;">
          <el-form-item label-width="120px" label="Education" prop="education">
            <el-input
              v-model="formPosition.education"
              placeholder=""
              disabled
            />
          </el-form-item>
          <el-form-item label="Report To" prop="reportTo">
            <el-input
              v-model="formPosition.reportTo"
              placeholder=""
              disabled
            />
          </el-form-item>
      </el-row>
    </el-form>
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="50px"
      @submit.native.prevent
      style="margin-top: 20px;">
      <el-form-item label="Name" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder=""
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          :disabled="formPosition.closed == 1"
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery">
          Search
        </el-button>
        <!--<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>-->
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          :disabled="formPosition.closed == 1"
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >Add</el-button>
      </el-col>
      <!--<el-col :span="1.5">-->
        <!--<el-button-->
          <!--type="success"-->
          <!--plain-->
          <!--icon="el-icon-edit"-->
          <!--size="mini"-->
          <!--:disabled="single"-->
          <!--@click="handleUpdate"-->
        <!--&gt;修改</el-button>-->
      <!--</el-col>-->
      <!--<el-col :span="1.5">-->
        <!--<el-button-->
          <!--type="danger"-->
          <!--plain-->
          <!--icon="el-icon-delete"-->
          <!--size="mini"-->
          <!--:disabled="multiple"-->
          <!--@click="handleDelete"-->
        <!--&gt;删除</el-button>-->
      <!--</el-col>-->
      <!--<el-col :span="1.5">-->
        <!--<el-button-->
          <!--type="warning"-->
          <!--plain-->
          <!--icon="el-icon-download"-->
          <!--size="mini"-->
          <!--@click="handleExport"-->
        <!--&gt;导出</el-button>-->
      <!--</el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      border
      v-loading="loading"
      :data="interviewList"
      @selection-change="handleSelectionChange">
      <el-table-column width="70px" label="Action" align="left" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            :disabled="formPosition.closed == 1"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleCandUpdate(scope.row)"
          >Edit</el-button>
          <br>
          <el-button
            :disabled="formPosition.closed == 1"
          size="mini"
          type="text"
          icon="el-icon-delete"
          v-hasPermi="['system:interview:del']"
          @click="handleDelete(scope.row)"
          >Del</el-button>
        </template>
      </el-table-column>
      <el-table-column label="Name" align="center" prop="name" />
      <el-table-column align="center" label="Date Of Interview">
        <el-table-column label="First" align="center" prop="firstTime">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.firstTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="Second" align="center" prop="secTime">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.secTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="Third" align="center" prop="thirdTime">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.thirdTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
      </el-table-column>

      <el-table-column label="Report Date" align="center" prop="reportDate">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.reportDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="Final Salary" align="center" prop="finalSalary" />
      <!--<el-table-column label="${comment}" align="left" prop="remark" />-->
      <el-table-column label="Status" align="center" prop="status" />
      <el-table-column label="Offer" align="center" prop="offer" />
      <!--<el-table-column label="${comment}" align="left" prop="createUserId" />-->
      <!--<el-table-column label="${comment}" align="left" prop="createTime" width="180">-->
        <!--<template slot-scope="scope">-->
          <!--<span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>-->
        <!--</template>-->
      <!--</el-table-column>-->
      <!--<el-table-column label="${comment}" align="left" prop="updateUserId" />-->
      <!--<el-table-column label="${comment}" align="left" prop="updateTime" width="180">-->
        <!--<template slot-scope="scope">-->
          <!--<span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>-->
        <!--</template>-->
      <!--</el-table-column>-->
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-form
      class="noteAdd2"
      :model="formPosition"
      ref="queryPositionForm"
      size="small"
      :inline="true"
      label-position="top"
      style="border-top: 2px solid #8c939d;margin-top: 40px;">
      <el-row style="margin-top: 20px;display: flex;">
        <el-col :span="12">
          <el-form-item
            label="Position Closed"
            prop="remarks"
            style="margin-top: 10px;width: 100%;">
            <!--<el-input-->
              <!--type="textarea"-->
              <!--:rows="8"-->
              <!--v-model="formPosition.remarks"></el-input>-->
            <el-select
              :disabled="formPosition.closed == 1"
              v-model="formPosition.remarks"
              placeholder="select">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item>
            <el-button
              :disabled="formPosition.closed == 1"
              type="primary"
              size="mini"
              @click="handleClosePosition">
              Close Position
            </el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <el-dialog
      title="Add InterView"
      :visible.sync="open"
      width="800px"
      append-to-body
    >
      <div style="display: flex;justify-content: center;margin-top: -30px;">
        <span style="color: #000;font-size: 18px;">Please Select A Candidate</span>
      </div>
      <el-form
        :model="queryParamsUser"
        ref="queryFormUser"
        size="small"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
        @submit.native.prevent
      style="margin-top: 20px;">
        <el-form-item label="Name" prop="name">
          <el-input
            v-model="queryParamsUser.name"
            placeholder=""
            clearable
            @keyup.enter.native="handleUserQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            :disabled="formPosition.closed == 1"
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleUserQuery">Search</el-button>
        </el-form-item>
      </el-form>
      <el-table
        :data="candidateList"
        style="margin-top: 0px;">
        <el-table-column width="60px;" label="action" align="left" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              :disabled="formPosition.closed == 1"
              size="mini"
              type="text"
              icon="el-icon-check"
              @click="handleAddUserCand(scope.row)"
            >Select</el-button>
          </template>
        </el-table-column>
        <el-table-column show-overflow-tooltip label="Name" align="left" prop="name" />
        <el-table-column show-overflow-tooltip label="Company" align="left" prop="ccompanyList[0].com" />
        <el-table-column show-overflow-tooltip label="Position" align="left" prop="ccompanyList[0].title" />
        <el-table-column show-overflow-tooltip label="Education" align="left" prop="schoolList[0].name" />
        <el-table-column show-overflow-tooltip width="60px" label="Age" align="left" prop="age" />
        <el-table-column show-overflow-tooltip label="salary" align="left" prop="salary" />
      </el-table>

      <pagination
        v-show="totalUser>0"
        :total="totalUser"
        :page.sync="queryParamsUser.pageNum"
        :limit.sync="queryParamsUser.pageSize"
        @pagination="getCandList"
      />
    </el-dialog>

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <!--<el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>-->
      <!---->
      <!--<div slot="footer" class="dialog-footer">-->
        <!--<el-button type="primary" @click="submitForm">确 定</el-button>-->
        <!--<el-button @click="cancel">取 消</el-button>-->
      <!--</div>-->
    <!--</el-dialog>-->
  </div>
</template>

<script>
import { listInterview, getInterview, delInterview, addInterview, updateInterview } from "@/api/system/interview";
import { listCandidate, getCandidate, delCandidate, addCandidate, updateCandidate } from "@/api/system/candidate";
import { listPosition, getPosition, delPosition, addPosition, updatePosition } from "@/api/system/position";

export default {
  name: "Interview",
  data() {
    return {
      options: [{
        value: 'Filled by other',
        label: 'Filled by other'
      }, {
        value: 'Cancelled',
        label: 'Cancelled'
      }, {
        value: 'Billed',
        label: 'Billed'
      }],
      value: '',
      candidateList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      totalUser: 0,
      positionId: null,
      // 【请填写功能名称】表格数据
      interviewList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      queryParamsUser: {
        pageNum: 1,
        pageSize: 10,
        name: null
      },
      positionList: [],
      queryPositionParams: {
        pageNum: 1,
        pageSize: 10,
        id: null
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        positionId: null,
        name: null
      },
      // 表单参数
      form: {},
      formPosition: {
        id: null,
        clientId: null,
        title: null,
        salary: null,
        salaryupper: null,
        reportTo: null,
        education: null,
        jobDuties: null,
        specialReq: null,
        assignedUserId: null,
        companyName: null,
        remarks: null,
        closed: 0
      },
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.positionId = this.$route.query.id;
    this.queryPositionParams.id = this.$route.query.id;
    this.queryParams.positionId = this.$route.query.id;
    this.getPositionList();
    this.getList();
    this.getCandList();
  },
  methods: {
    /** 查询【请填写功能名称】列表 */
    getPositionList() {
      listPosition(this.queryPositionParams).then(response => {
        this.positionList = response.rows;
        if (this.positionList != null) {
          this.formPosition = this.positionList[0];
        }
      });
    },
    getCandList() {
      // this.loading = true;
      listCandidate(this.queryParamsUser).then(response => {
        this.candidateList = response.rows;
        this.totalUser = response.total;
        // this.loading = false;
      });
    },
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true;
      listInterview(this.queryParams).then(response => {
        this.interviewList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        firstTime: null,
        secTime: null,
        thirdTime: null,
        reportDate: null,
        finalSalary: null,
        remark: null,
        status: null,
        offer: null,
        createUserId: null,
        createTime: null,
        updateUserId: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    handleClosePosition(){
      this.formPosition.closed = 1;
      updatePosition(this.formPosition).then(response => {
        this.$modal.msgSuccess("success");
      }).error(e => {
        this.$modal.msgError(e.message);
      });
    },
    handleUserQuery() {
      this.queryParamsUser.pageNum = 1;
      this.getCandList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加【请填写功能名称】";
    },
    handleAddUserCand(row){
      this.open = false;
      this.$router.push({path:'/system/interview/add',
        query: {candId: row.id, positionId: this.positionId, interViewId: null}});
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getInterview(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改【请填写功能名称】";
      });
    },
    handleCandUpdate(row){
      this.$router.push({path:'/system/interview/add',
        query: {candId: row.userId, positionId: this.positionId, interViewId: row.id}});
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateInterview(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInterview(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('Delete？').then(function() {
        return delInterview(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("success");
      }).catch(() => {});
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
  .noteAdd .el-form-item__label {
    line-height: 0px;
  }
  .noteAdd2 .el-form-item__label {
    line-height: 0px;
  }
  .noteAdd .el-input__inner {
    border-top-width: 0px;
    border-left-width: 0px;
    border-right-width: 0px;
    border-bottom-width: 1px;
    /*outline: medium;*/
  }
  .noteAdd .el-form-item label:after {
    content: "";
    display: inline-block;
    width: 100%;
  }

  /*.noteAdd .el-form-item__label {*/
    /*text-align: justify;*/
    /*height: 50px;*/
  /*}*/

  /*.noteAdd .el-form-item.is-required .el-form-item__label:before {*/
    /*content: none !important;*/
  /*}*/

  .btn-interview {
    position: absolute;
    top: 10px;
    right: 150px;
    height: 250px;
    width: 250px;
    font-size: 8px;
    z-index: 9999;
  }

</style>
